package com.junzhiit.nengji.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.bo.ShopsLocatedIntegralBO;
import com.junzhiit.nengji.dao.ShopsLocatedIntegralDAO;
import com.junzhiit.nengji.model.ShopsLocatedIntegral;

@Repository
public class ShopsLocatedIntegralDAOImpl extends AbstractBaseDAO<ShopsLocatedIntegral> implements ShopsLocatedIntegralDAO{
	
	@Override
	public List<ShopsLocatedIntegralBO> findAll(ShopsLocatedIntegral shopsLocatedIntegral,int pageNumber, int maxPageRow){
		StringBuffer sql=new StringBuffer();
		sql.append(" select sli.*,(case when s.`name` is null then sl.shop_name else s.`name` end) shopName,");
		sql.append(" (select a.`name` from area a where a.id=s.area_province_id) province_name,");
		sql.append(" (select a.`name` from area a where a.id=s.area_city_id) city_name,");
		sql.append(" (select a.`name` from area a where a.id=s.area_county_id) county_name,");
		sql.append(" s.address,s.linkman,s.mobile_phone,sl.regist_status from shops_located_integral sli");
		sql.append(" inner join shops_located sl on sli.shops_located_id=sl.id");
		sql.append(" left join shop s on s.id=sl.shop_id");
		sql.append(" where sli.is_delete=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(shopsLocatedIntegral.getUserName())){
			sql.append(" and sli.user_name like ?");
			param.setString("%" + shopsLocatedIntegral.getUserName() + "%");
		}
		
		sql.append(" order by sl.created_time desc ");
		return super.queryForPagingList(sql.toString(),ShopsLocatedIntegralBO.class,param, pageNumber, maxPageRow);
	}
	
	@Override
	public int getTotalCount(ShopsLocatedIntegral shopsLocatedIntegral){
		StringBuffer sql=new StringBuffer();
		sql.append(" select count(*) from shops_located_integral sli");
		sql.append(" inner join shops_located sl on sli.shops_located_id=sl.id");
		sql.append(" left join shop s on s.id=sl.shop_id");
		sql.append(" where sli.is_delete=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(shopsLocatedIntegral.getUserName())){
			sql.append(" and sli.user_name like ?");
			param.setString("%" + shopsLocatedIntegral.getUserName() + "%");
		}
		sql.append(" order by sl.created_time desc ");
		return super.queryForInt(sql.toString(),param);
	}

	@Override
	public ShopsLocatedIntegral getShopsLocatedIntegral(String shopsLocatedId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from shops_located_integral");
		sql.append(" where is_delete=? and shops_located_id =?");
		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());
		params.setString(shopsLocatedId);
		return super.query(sql.toString(),ShopsLocatedIntegral.class,params);
	}
	
	
}
